package com.company.project.web.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.project.common.pojo.Rest;
import com.company.project.web.finance.pojo.bill.Bill;
import com.company.project.web.finance.mapper.BillMapper;
import com.company.project.web.finance.pojo.bill.BillForm;
import com.company.project.web.finance.pojo.bill_serviceitem.BillServiceitem;
import com.company.project.web.finance.service.BillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.project.web.system.mapper.TechnicianServiceitemMapper;
import com.company.project.web.system.mapper.TechnicianSortMapper;
import com.company.project.web.system.mapper.UserMapper;
import com.company.project.web.system.pojo.serviceitem.SelectedServiceitemDO;
import com.company.project.web.system.pojo.technician_serviceitem.TechnicianServiceitem;
import com.company.project.web.system.pojo.technician_sort.TechnicianSort;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 任建波
 * @since 2019-09-10
 */
@Service
public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements BillService {

    @Autowired
    private BillMapper billMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    TechnicianSortMapper technicianSortMapper;
    @Autowired
    TechnicianServiceitemMapper technicianServiceitemMapper;


    /**
     *下单
     */
    @Override
    @Transactional
    public Rest placeOrder(BillForm billForm) {
        Bill bill = new Bill();
        BeanUtils.copyProperties(billForm, bill);

        //  排到最前面的技师
        QueryWrapper<TechnicianSort> firstSortTechnicianQueryWrapper = new QueryWrapper<>();
        firstSortTechnicianQueryWrapper.eq("previous_technician_id", null);
        TechnicianSort firstSortTechnician = technicianSortMapper.selectOne(firstSortTechnicianQueryWrapper);
        //  排到最后面的技师
        QueryWrapper<TechnicianSort> lastSortTechnicianQueryWrapper = new QueryWrapper<>();
        lastSortTechnicianQueryWrapper.eq("previous_technician_id", null);
        TechnicianSort lastSortTechnician = technicianSortMapper.selectOne(lastSortTechnicianQueryWrapper);

        //技师会的服务项目
        QueryWrapper<TechnicianServiceitem> technicianServiceitemQueryWrapper = new QueryWrapper<>();
        technicianServiceitemQueryWrapper.eq("technician_id", firstSortTechnician.getTechnicianId());
        List<TechnicianServiceitem> technicianServiceitemList = technicianServiceitemMapper.selectList(technicianServiceitemQueryWrapper);
        //从technicianServiceitemList集合中，取出字段serviceitemId的列表
        List<Integer> serviceitemIdList = technicianServiceitemList.stream().map(item -> item.getServiceitemId()).collect(Collectors.toList());

        //  订单的服务项目
        List<BillServiceitem> billServiceitemList = new ArrayList<>();
        for (SelectedServiceitemDO selectedServiceitemDO: billForm.getSelectedServiceitemDOS()){
            //  没有指定技师
            if(selectedServiceitemDO.getTechnicianId() == null){
                //  如果技师会做此服务项目，选择该技师，改技师排序到最后
                if(serviceitemIdList.contains(selectedServiceitemDO.getServiceitemId())){
                    BillServiceitem billServiceitem = setBillServiceitem(bill.getId(),selectedServiceitemDO.getServiceitemId(), firstSortTechnician.getTechnicianId());

                    //  修改该技师排序
                    firstSortTechnician.setPreviousTechnicianId(lastSortTechnician.getTechnicianId());   //该技师前面的技师为当前最后面技师的技师id
                    firstSortTechnician.setNextTechnicianId(null);   //该技师后面技师id为空
                    technicianSortMapper.updateById(firstSortTechnician);

                    //  修改最后一个技师排序
                    lastSortTechnician.setNextTechnicianId(firstSortTechnician.getTechnicianId());   //将该技师拍到当前最后一个技师后面
                    technicianSortMapper.updateById(lastSortTechnician);

                    billServiceitemList.add(billServiceitem);
                }
            } else {    //  选择指定技师， 不动钟
                BillServiceitem billServiceitem = setBillServiceitem(bill.getId(),selectedServiceitemDO.getServiceitemId(), selectedServiceitemDO.getTechnicianId());

                billServiceitemList.add(billServiceitem);
            }
        }

        return null;
    }

    /**
     *  订单服务项目
     * @param billId 订单id
     * @param serviceitemId 服务项目id
     * @param technicianId 技师id
     */
    private BillServiceitem setBillServiceitem(Integer billId, Integer serviceitemId, Integer technicianId){
        BillServiceitem billServiceitem = new BillServiceitem();
        billServiceitem.setBillId(billId); //订单id
        billServiceitem.setServiceitemId(serviceitemId);  // 服务项目id
        billServiceitem.setTechnicianId(technicianId);    // 技师id

        return billServiceitem;
    }

}
